<?php
/**
 * 数据字典服务层
 * @author maniac
 */
namespace app\common\service;

class Dictionary extends Base
{
    /**
     * 导出数据字典
     */
    public function export()
    {
        $sql    = "SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,COLUMN_KEY,EXTRA,COLUMN_COMMENT,CHARACTER_SET_NAME,COLLATION_NAME FROM information_schema. COLUMNS WHERE TABLE_SCHEMA = '".config('database.database')."'";
        $rows   = \think\Db::query($sql);
        $result = [];
        foreach ($rows as $v) {
            // $result[$v['TABLE_NAME']][] = $v;
            $temp     = \think\Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='".config('database.database')."' AND TABLE_NAME='{$v['TABLE_NAME']}' LIMIT 1");
            $result[$v['TABLE_NAME']]['header']  = $temp[0]['TABLE_COMMENT'].' '.$v['TABLE_NAME'];
            $result[$v['TABLE_NAME']]['value'][] = [
                'A' => $v['COLUMN_NAME'],
                'B' => strtoupper($v['COLUMN_TYPE']),
                'C' => $v['IS_NULLABLE'],
                'D' => $v['COLUMN_DEFAULT'],
                'E' => $v['COLUMN_KEY'],
                'F' => $v['COLUMN_COMMENT'],
            ];
        }
        
        $title = [
            'A' => '字段',
            'B' => '类型',
            'C' => '是否为空',
            'D' => '默认值',
            'E' => '索引',
            'F' => '注释',
        ];
        
        $objPHPExcel = new \PHPExcel();
        $flag        = true;
        $i           = 1;
        while ($flag) {
            foreach ($result as $key => $val) {
                // 表头，合并单元格
                $objPHPExcel->getActiveSheet()->mergeCells('A'.$i.':F'.$i);
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $val['header']);
                $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->getColor()->setRGB('0070C0');
                $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setName('Microsoft YaHei');
                $i++;
                
                // 表格标题
                foreach ($title as $k => $v) {
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $v);
                    $objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth(30);
                    $objPHPExcel->getActiveSheet()->getStyle($k.$i)->getFont()->getColor()->setRGB('0070C0');
                    $objPHPExcel->getActiveSheet()->getStyle($k.$i)->getFont()->setName('Microsoft YaHei');
                }
                $i++;
                
                if ($val['value']) {
                    foreach ($val['value'] as $k => $v) {
                        foreach ($v as $k1 => $v1) {
                            $objPHPExcel->getActiveSheet()->getStyle($k1.$i)->getFont()->setName('Microsoft YaHei');
                            $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($k1.$i, $v1, \PHPExcel_Cell_DataType::TYPE_STRING);
                        }
                        $i++;
                    }
                } else {
                    $i++;
                }
                $i++;
            }
            $flag = false;
        }
        
        $objPHPExcel->getActiveSheet()->setTitle('Simple');
        $objPHPExcel->setActiveSheetIndex(0);
        
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="数据字典.xls"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}
